<!DOCTYPE HTML>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="__UI__/css/bs3/dpl.css" rel="stylesheet">
<link href="__UI__/css/bs3/bui.css" rel="stylesheet">
<link href="__UI__/css/page.css" rel="stylesheet" type="text/css" />
</head>
<body> 
<div class="container">
	<div class="search-grid-container">
    	<form id="J_Form" class="form-horizontal registerform">
            <div id="top" style="display:none; margin-bottom:20px;">
                <input type="text" name="title" id="title" style="width:250px;">
                <button type="button" class="button button-primary" id="add" onClick="addFunction();">新建部门</button>
                <button type="button" class="button button-info" id="cancel" onClick="cancelFunction();">新建取消</button>
            </div>
        </form>
	    <div id="grid"></div>
	</div>   
</div>
<script type="text/javascript" src="__UI__/js/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="__UI__/js/bui.js"></script>
<script type="text/javascript" src="__UI__/js/config.js"></script>
<script type="text/javascript">
	BUI.use('common/page');
	BUI.use(['common/search','bui/overlay'],function (Search,Overlay){
		var columns = [
			{title:'部门名称',dataIndex:'title',width:100},
			{title:'数据操作',dataIndex:'',width:200,renderer:function(value,obj){
				var editStr = Search.createLink({
					id:'department_edit' + obj.id,
					title:'部门编辑',
					text:'编辑',
					href:'{:U("Home/Department/edit/id/' + obj.id + '")}',
					isClose:true,
				}),
				delStr = '<span class="grid-command btn-del" title="部门删除">删除</span>';
				Search.createLink;
				return editStr + delStr;
			}}
		],
		store = Search.createStore('{:U("Department/index")}',{
			proxy:{
				save:{removeUrl:'{:U("Department/index")}'}
			},
			autoSync:true,
			pageSize:10,
			
		}),
		gridCfg = Search.createGridCfg(columns,{
			forceFit:true,
			tbar:{
				items:[
					{text:'<i class="icon-refresh"></i>刷新页面',btnCls:'button button-small',handler:function(){
						window.location.reload();
					}},
					{text:'<i class="icon-plus"></i>新建部门',btnCls:'button button-small',handler:addFunction},
					{text:'<i class="icon-remove"></i>批量删除',btnCls:'button button-small',handler:delFunction},
				]
			},
			plugins:[BUI.Grid.Plugins.CheckSelection,BUI.Grid.Plugins.RowNumber,BUI.Grid.Plugins.CellEditing] 
		});
		var search = new Search({
			store:store,
			gridCfg:gridCfg,
		}),
		grid = search.get('grid');
		function delFunction(){
			var selections = grid.getSelection();
			delItems(selections);
		}
		function delItems(items){
			var ids = [];
			BUI.each(items,function(item){
				ids.push(item.id);
			});
			if(ids.length){
				BUI.Message.Confirm('确认要删除选中的记录么？',function(){
					$.ajax({
						url:'{:U("Department/del")}',
						dataType:'json',
						data:{ids : ids},
						success:function(data){
							if(data.status == 'y'){
								search.load();
							}else{
								BUI.Message.Alert('删除失败！');
							}
						}
					});
				},'question');
			}
		}
		grid.on('cellclick',function(ev){
			var sender = $(ev.domTarget);
			if(sender.hasClass('btn-del')){
				var record = ev.record;
				delItems([record]);
			}
		});
	});
	function addFunction(){
		$('#top').show();
	}
	$(function(){
		$('#add').click(function(){
			var u = {:session(C('AUTH_KEYS'))};
			var t = $('#title').val();    
			if(t == ''){
				BUI.Message.Alert('请输入部门名称！');
			}else{
				$.ajax({
					type:'post',
					url:'{:U("Home/Department/add")}',
					dataType:'json',
					data:{title:t,uid:u},
					success:function(data){
						if(data.status == 'y'){
							$('#top').hide();
							window.location.reload();
						}else{
							BUI.Message.Alert('新建失败！');
						}
					}
				});
			}
		});
	});
	$(function () {       
		$('#cancel').click(function(){
			var top = $('#top').hide();
			if(top){
				$("#title").attr("value",'');
			}
		});
	});
</script>
</body>
</html>